package org.ax.service.employ.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.employ.EmployeeMapper;
import org.ax.pojo.employ.Department;
import org.ax.pojo.employ.Employee;
import org.ax.service.employ.DepartmentService;
import org.ax.service.employ.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-03-02-22:10
 */
@Service
public class IEmployeeService extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    DepartmentService departmentService;
    @Override
    public Page<Employee> findEmployeeList(Page page,String departmentId) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isBlank(departmentId), "department_id",departmentId);
        Page page1 = getBaseMapper().selectPage(page, queryWrapper);
        List<Employee> employees=(List<Employee>)page1.getRecords();
        for (Employee employee : employees) {
            employee.setDepartment(departmentService.toDepartmentId(employee.getDepartmentId()));
        }
        page1.setRecords(employees);
        return page1;
    }

    @Override
    public boolean addEmployee(Employee employee) {
        return getBaseMapper().insert(employee)>0;
    }

    @Override
    public boolean updateEmployee(Employee employee) {
        return getBaseMapper().updateById(employee)>0;
    }

    @Override
    public boolean deletesEmployee(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public Employee toEmployeeId(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<Employee> availableEmployee() {
        return getBaseMapper().selectList(null);
    }
}
